const http = require('http');
const server = http.createServer();
server.listen(3000,()=>{
  console.log('服务器已启动');
});
const fs = require('fs');
const path = require('path');
const template = require('art-template');
let dataArr = {list: [
  {goods_name:"阳光美包新款单肩包女包时尚子母包四件套女", goods_price:"116.00", goods_pic:"uploads/like_01.png"},
  {goods_name:"爱仕达 30CM炒锅不粘锅NWG8330E电磁炉炒", goods_price:"1161.00", goods_pic:"uploads/like_02.png"},
  {goods_name:"捷波朗蓝牙耳机", goods_price:"116.00", goods_pic:"uploads/like_03.png"},
  {goods_name:"大方块，不知道是干什么用的", goods_price:"2116.00", goods_pic:"uploads/like_04.png"},
  {goods_name:"三星（G5500）", goods_price:"1816.00", goods_pic:"uploads/like_05.png"},
  {goods_name:"好像是个洗手液", goods_price:"3116.00", goods_pic:"uploads/like_06.png"}
]};
server.on('request',(req,res)=>{
  res.sendFile = function(fpaht){
    fs.readFile(fpaht,(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
      res.end(data);
    })
  }
  const url = req.url;
  if(url === '/index'){
    const str = template(path.join(__dirname,'view/index.html'),dataArr);
    res.end(str);
  }else if(url == '/list'){
    res.sendFile('view/list.html');
  }else if(url == '/login'){
    res.sendFile('view/login.html');
  }else if(url == '/item'){
    res.sendFile('view/item.html');
  }else if(url == '/cart'){
    res.sendFile('view/cart.html');
  }else if(url == '/register'){
    res.sendFile('view/register.html');
  }else if(url.startsWith('/style')){
    res.sendFile('view'+url);
  }else if(url.startsWith('/uploads')){
    res.sendFile('view'+url);
  }else if(url.startsWith('/images')){
    res.sendFile('view'+url);
  }else if(url.startsWith('/fonts')){
    res.sendFile('view'+url);
  }
})